package com.myhome.controller;

import com.myhome.entity.Admin;
import com.myhome.entity.RespBean;
import com.myhome.entity.userLoginParams;
import com.myhome.service.IAdminService;
import com.myhome.service.IRoleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.security.Principal;

@RestController
@RequestMapping("user")
@Api(tags = "UserLoginController")
public class UserLoginController {
    @Autowired
    IAdminService service;
    @Autowired
    IRoleService roleService;

    @ApiOperation(value = "登录")
    @PostMapping("/login")
    public RespBean login(@RequestBody userLoginParams user, HttpServletRequest request) {
        return service.login(user.getUsername(), user.getPassword(),user.getCaptcha(), request);
    }

    @ApiOperation(value = "退出登录")
    @PostMapping("logout")
    public RespBean logOut(){
        return RespBean.success("退出成功");
    }

    @ApiOperation(value = "获取当前登录用户的信息")
    @GetMapping("/getInfo")
    public Admin getUsetInfo(Principal principal){
        Admin user = service.getUserInfo(principal.getName());
        user.setPassword(null);
        Integer adminId = user.getId();
        user.setRoles(roleService.getRolesByUserId(adminId));
        return user;
    }
}
